import axios from 'axios'
import { Loading } from 'element-ui'

import NProgress from 'nprogress' // 引入nprogress插件
import 'nprogress/nprogress.css'  // 这个nprogress样式必须引入

const http = axios.create({
    baseURL: 'http://localhost:3000/api'
})

let loading
let loadingCount = 0

http.interceptors.request.use(config => {
    NProgress.start() // 设置加载进度条(开始..)
    loadingCount++
    loading = Loading.service({
    target: 'body',
    background: 'transparent',
    text: '载入中',
    })
    // 在最后必须return config
    return config
})

http.interceptors.response.use(config => {
    NProgress.done() // 设置加载进度条(结束..)
    loadingCount--
    if (loadingCount<=0) {
        loading.close()
    } else {
       
    }
    return config
})

export default http